﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;
using Num2Wrd;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;

namespace Fitness_club
{
    public partial class fees : Form
    {
        private int memberId;

        private Boolean feesArdyPaid = false;

        private DateTime dueDate;

        private int planDuration = -1;

        Main parent;

        public static string constr = ConfigurationManager.AppSettings["ConStr"];
        
        SqlConnection con = new SqlConnection(constr);

        public fees(int memberId)
        {
            InitializeComponent();
            fetchBill();
            this.memberId = memberId;
            fetchMemberById(this.memberId);
        }

        public fees(Main ref_parent)
        {
            parent = ref_parent;
            InitializeComponent();
        }
        private void fees_Load(object sender, EventArgs e)
        {
           // in this form load
           // first obtained first name + last name
           // put them in to combobox1

            buttonPrint.Enabled = false;
        }


        private void fetchMemberById(int id)
        {
            try
            {
                SqlCommand sqlCommand = new SqlCommand("select m.*,sp.type,sp.amount,sp.duration from  member m LEFT JOIN subscription_plan sp ON m.mem_plan=sp.id where m.id=" + id, con);
                con.Open();
                SqlDataReader reader = sqlCommand.ExecuteReader();

                if (reader != null)
                {
                    while (reader.Read())
                    {
                        textBoxName.Text = reader["firstname"].ToString();

                        textBoxMemId.Text = reader["id"].ToString();

                        textBoxPlanType.Text = reader["type"].ToString();

                        String planAmt=reader["amount"].ToString();
                        
                        textBoxAmount.Text = planAmt;

                        NumberToEnglish num=new NumberToEnglish();

                        textBoxAmtInWords.Text = num.changeCurrencyToWords(planAmt);

                        planDuration = int.Parse(reader["duration"].ToString());
                        
                        String dueDateString = reader["due_date"].ToString();

                        if (dueDateString != null && dueDateString.Length>0)
                        {
                            feesArdyPaid = true;
                            try
                            {
                                dueDate = DateTime.Parse(dueDateString);

                                String subcriptionPeriod = dueDate.ToString(Constants.DATE_FORMAT) + " - " + dueDate.AddMonths(planDuration).ToString(Constants.DATE_FORMAT);
                                textBoxSubcriptionPeriod.Text = subcriptionPeriod;
                            }
                            catch (Exception)
                            {

                                MessageBox.Show("Fetch Member by ID is Failed, Try Again", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                }

            }
            finally
            {
                con.Close();
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //according to selected combobox1 set the values for combobox2
            // that is the plan type accepted by member (First name + Last Name)

             

        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    

        private void updateDueDate()
        {
            try
            {
                DateTime dueDateTime;
                DateTime lastPadiDateTime;

                if (feesArdyPaid)
                {
                    lastPadiDateTime = dueDate;
                    dueDateTime = dueDate.AddMonths(planDuration);

                }
                else
                {
                    DateTime today = DateTime.Today;
                    dueDateTime = today;
                    lastPadiDateTime = today;
                }
                if (dueDateTime != null)
                {
                    SqlCommand updateDueDateCommand = new SqlCommand("update member set due_date=@dueDate,last_paid_date=@lastPaidDate where id=" + memberId, con);
                    updateDueDateCommand.Parameters.Add(new SqlParameter("@dueDate", dueDateTime));
                    updateDueDateCommand.Parameters.Add(new SqlParameter("@lastPaidDate", lastPadiDateTime));
                    con.Open();
                    updateDueDateCommand.ExecuteNonQuery();
                    con.Close();
                }
            }
            catch (Exception ecp)
            {
                MessageBox.Show("Update Due Date is failed...!, Try Again", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
           
           
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            try
            {
                // printBill();
                int billId = -1;
                SqlCommand command = new SqlCommand("SELECT MAX(id) as id from fees_transaction", con);

                // SqlCommand command = new SqlCommand("SELECT * from fees_transaction where id", con);
                con.Open();
                SqlDataReader reader = command.ExecuteReader();
                if (reader != null)
                {
                    while (reader.Read())
                    {
                        billId = int.Parse(reader["id"].ToString());
                    }
                }

                int invoidNo = int.Parse(textBoxInvoiceNo.Text);
                if (billId == invoidNo)
                {
                    bill_for_fees add = new bill_for_fees(billId, this.memberId);
                    add.Visible = true;
                    add.MdiParent = parent;
                    add.Show();
                    this.Hide();
                }
                con.Close();
            }
            catch (Exception ep)
            {
                MessageBox.Show("Bill Printing is faild, Try Again", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
           
        }

        private void fetchBill()
        {
            try
            {
                SqlCommand command = new SqlCommand("SELECT isnull(max(id),0)+1 as id from fees_transaction", con);

                // SqlCommand command = new SqlCommand("SELECT * from fees_transaction where id", con);
                con.Open();
                SqlDataReader reader = command.ExecuteReader();
                if (reader != null)
                {
                    while (reader.Read())
                    {
                        int billNo = int.Parse(reader["id"].ToString());
                        textBoxInvoiceNo.Text = billNo.ToString();
                    }
                }
                con.Close();
            }
            catch (Exception er)
            {
                MessageBox.Show("Fetching Bill ID Failed, Try Again", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }

        private void buttonPay_Click(object sender, EventArgs e)
        {
           
            try
            {
                DateTime today = DateTime.Today;
                SqlCommand insertFeesHirstory = new SqlCommand("insert into fees_transaction values(" + this.memberId + ",'" + textBoxName.Text + "','" + textBoxPlanType.Text + "','"+textBoxSubcriptionPeriod.Text+"',@dateOfTransaction,'" + textBoxAmount.Text + "')", con);
                insertFeesHirstory.Parameters.Add(new SqlParameter("@dateOfTransaction",today));
                con.Open();
                insertFeesHirstory.ExecuteNonQuery();
                con.Close();
                updateDueDate();
                MessageBox.Show("Fees paid successfully", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                buttonPrint.Enabled = true;
                buttonPay.Enabled = false;

            }
            catch
            {
                MessageBox.Show("Fees paid Failed, Try Again", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Hide();
            }
            
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
        
        
    }
}
